<?php
/**
 * Instantiates and make a classMapper
 * 
 * @author Youssef Driouach
 *
 */


class Application_Model_Factory_ObjectMapperFactory {
	
	/**
	 * load the mapping of the class from it's name.
	 * 
	 * @param string $class
	 * @throws RuntimeException
	 * @return classMapper
	 */
	public static function load($class)
	{
		
		$mapperclasse = 'Application_Model_' . $class.'Mapper';
		$objectClass = 'Application_Model_' . $class;
	
		if (file_exists($chemin = '..\\application\\models\\'.$class . 'Mapper.php'))
		{
			//require $chemin;
			$mapper = new $mapperclasse;
			return $mapper;
		}
		else
		{
			throw new RuntimeException('La classe <strong>' . $mapperclasse . '</strong> n\'existe pas !');
		}
	}
	
	/**
	 * Find the object in the data base from the id_object.
	 * @param int $id_object
	 * @param string $class
	 * @return object if exist else null
	 */
	public static function find($id_object,$class)
	{
		try {
			$objectMapper = Application_Model_Factory_ObjectMapperFactory::load($class);
			$object = Application_Model_Factory_ObjectFactory::load($class);
			$object = $objectMapper->find($id_object, $object);
			if(is_object($object))
			{
				return $object;
			}
			else {
				return;
			}
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		
	}
}

